package com.ioc.condition;

import com.sun.media.sound.SoftTuning;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @author cyt
 * @create 2020-07-29 下午 11:32
 */
public class WindowsCondition implements Condition {

    /**
     * @param context   ： 当前环境的上下文信息
     * @param metadata： 注解的信息
     * @return
     */
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // 1 获得类加载器
        ClassLoader classLoader = context.getClassLoader();
        // 2 获得运行时候的环境
        Environment environment = context.getEnvironment();
        // 3 获得ioc 的注册类
        BeanDefinitionRegistry registry = context.getRegistry();
        // 4 获得当前 ioc 的bean工厂
        ConfigurableListableBeanFactory factory = context.getBeanFactory();
        String osname = environment.getProperty("os.name");

        boolean user = registry.containsBeanDefinition("user");
        System.out.println(user);
        if (osname.contains("Windows")) {
            return true;
        }
        return false;
    }
}
